﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Medicae_Toolkit
{
    public partial class MedicaeEditor : Form
    {
        Churigeon Me;
        Churigeon Orig;
        PMedicae PForm;
        bool isnew;
        public MedicaeEditor(PMedicae p, Churigeon c, bool New)
        {
            InitializeComponent();
            Orig = c;
            Me = c;
            PForm = p;
            isnew = New;
            if (!New)
            {
                nameBox.Text = c.name;
                intstatBox.Text = c.IntStat.ToString();
                tenBox.Checked = c.plusTen;
                twentyBox.Checked = c.plusTwenty;
                talented.Checked = c.Talented;
                mastchur.Checked = c.MasterChur;
            }
        }

        private void End(object sender, EventArgs e)
        {
            PForm.UpdateList();
            PForm.BringToFront();
            Close();
        }

        private void CharEditor_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString() == Keys.Enter.ToString())
            {
                saveButton_Click(sender, e);
            }
            if (e.KeyChar.ToString() == Keys.Escape.ToString())
            {
                End(sender, e);
            }
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            if (Me.name == "")
            {
                MessageBox.Show("Please enter a Name!");
                return;
            }
            if (Me.IntStat == -1)
            {
                MessageBox.Show("Please enter the Intelligence Stat!");
                return;
            }
            
            if (isnew)
            {
                PForm.PForm.Churigeons.Add(Me);
            }
            else
            {
                PForm.PForm.Churigeons.Remove(Orig);
                PForm.PForm.Churigeons.Add(Me);
            }
            End(sender, e);
        }

        private void nameBox_TextChanged(object sender, EventArgs e)
        {
            Me.name = nameBox.Text;
        }

        private void woundsBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                Me.IntStat = Convert.ToInt32(intstatBox.Text);
            }
            catch (FormatException fe)
            {
                return; // They must have attempted to Type in text instead of a number;
            }
            
        }

        private void CharEditor_Load(object sender, EventArgs e)
        {

        }

        private void tenBox_CheckedChanged(object sender, EventArgs e)
        {
            Me.plusTen = tenBox.Checked;
        }

        private void twentyBox_CheckedChanged(object sender, EventArgs e)
        {
            Me.plusTwenty = twentyBox.Checked;
        }

        private void talented_CheckedChanged(object sender, EventArgs e)
        {
            Me.Talented = talented.Checked;
        }

        private void mastchur_CheckedChanged(object sender, EventArgs e)
        {
            Me.MasterChur = mastchur.Checked;
        }

        private void MedicaeEditor_FormClosed(object sender, FormClosedEventArgs e)
        {
            PForm.UpdateList();
            PForm.Show();
            PForm.BringToFront();
        }
    }
}
